﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL.JObject;

namespace Framework.Portal.Layouts.Admin.Components.Lang
{
    public partial class LangManagement : BaseControl
    {
        public string MENUNAME = string.Empty;

        public LangManagement()
        {
        }

        public LangManagement(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadLang();
                lblCommandName.Text = MENUNAME;
            }
        }

        protected void dgrPortals_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int langId = Convert.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "editlang":
                    LoadData(langId);
                    break;
                case "deletelang":
                    ManagerFactory.DAOEntity.Delete(new PortalLanguage { ID = langId });
                    lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
                    LoadLang();
                    break;
                default:
                    break;
            }
        }

        protected void dgrPortals_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                var btnDeleteRole = (ImageButton) e.Item.FindControl("btnDeletePortal");
                btnDeleteRole.OnClientClick = Localization.GetText("MESSAGE", "MSG_DELETE_CONFIRM");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            PortalLanguage _portalInfo = GetData();
            _portalInfo.ID = Convert.ToInt32(lblId.Text);
            ManagerFactory.DAOEntity.Update((_portalInfo));
            lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            LoadLang();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ManagerFactory.DAOEntity.Insert(GetData());
            lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            LoadLang();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtName.Text = string.Empty;
            txtCulture.Text = string.Empty;
        }

        private PortalLanguage GetData()
        {
            var portalInfo = new PortalLanguage();
            portalInfo.LANGUAGE_NAME = txtName.Text;
            portalInfo.LANGUAGE_CULTURE = txtCulture.Text;
            return portalInfo;
        }

        private void LoadLang()
        {
            List<PortalLanguage> lstLang = ManagerFactory.DAOEntity.GetList<PortalLanguage>(new PortalLanguage());
            dgrPortals.DataSource = lstLang;
            dgrPortals.DataBind();
        }

        private void LoadData(int lang)
        {
            PortalLanguage info = ManagerFactory.DAOEntity.GetInfo<PortalLanguage>(new PortalLanguage() { ID = lang });
            if (info != null)
            {
                lblId.Text = info.ID.ToString();
                txtName.Text = info.LANGUAGE_NAME;
                txtCulture.Text = info.LANGUAGE_CULTURE;
            }
            else
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_ERROR");
        }
    }
}